package queries;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import entitys.Employee;
import entitys.LoginUser;
import enums.Position;
import enums.Result;
import enums.Status;

/**
 * updates the status of the employee that logged in to 'LOGGED'
 * @author Dima
 *
 */
public class GetUsersQuery {

	@SuppressWarnings("resource")
	public static Employee exe(LoginUser loginUser,Connection conn) {
		ResultSet res = null; 
		Statement stmt;
		Employee e =null;
		try {
			stmt = conn.createStatement();
			res = stmt.executeQuery("SELECT * FROM ccrm.employee WHERE employee.userid='"+ loginUser.getUserId() +"' AND employee.pwd='"+ loginUser.getPassword() + "';");
			
			if(!res.next()) return null; //return Result.IDNOTEXIST;
			else
			{
				try{
					stmt = conn.createStatement();
					stmt.executeUpdate("UPDATE ccrm.employee SET employee.status = 'LOGGED' WHERE userid = '"+ loginUser.getUserId() +"';");
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
					return null;
			}
			e = new Employee(res.getString(1),  res.getString(2), res.getString(6), res.getString(7), res.getString(8), res.getString(9), res.getString(10), "",Position.valueOf(res.getString(4)),"");
			e.setStatus(Status.valueOf(res.getString(3)));
			res.close();

			}	
			
			
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			return null;
		}
		
		return e;
		
	}
}
